#!/bin/bash
# MES系统启动脚本

cd /home/ubuntu/zlymes/mesmaster

echo "=== MES系统启动脚本 ==="
echo "当前目录: $(pwd)"
echo "当前时间: $(date)"

# 修复兼容性问题
echo "步骤1: 修复Django兼容性问题"
python fix_all_compatibility.py

# 检查Python环境
echo "步骤2: 检查Python环境"
python --version

# 检查Django版本
echo "步骤3: 检查Django版本"
python -c "import django; print('Django版本:', django.VERSION)"

# 运行数据库迁移
echo "步骤4: 运行数据库迁移"
python manage.py migrate --settings=settings_mes_basic

# 创建超级用户
echo "步骤5: 创建超级用户"
python manage.py shell --settings=settings_mes_basic << EOF
from django.contrib.auth.models import User
if not User.objects.filter(username='admin').exists():
    User.objects.create_superuser('admin', 'admin@example.com', 'admin123')
    print('超级用户创建成功')
else:
    print('超级用户已存在')
EOF

# 启动服务器
echo "步骤6: 启动MES服务器"
echo "启动基础MES服务 (端口8004)..."
nohup python manage.py runserver 0.0.0.0:8004 --settings=settings_mes_basic > /tmp/mes_8004.log 2>&1 &
echo "基础MES服务启动完成，PID: $!"

echo "启动完整MES服务 (端口8005)..."
nohup python manage.py runserver 0.0.0.0:8005 --settings=settings_full_mes > /tmp/mes_8005.log 2>&1 &
echo "完整MES服务启动完成，PID: $!"

echo "启动Django管理服务 (端口8006)..."
nohup python manage.py runserver 0.0.0.0:8006 --settings=settings_django_with_mes > /tmp/mes_8006.log 2>&1 &
echo "Django管理服务启动完成，PID: $!"

echo "=== 服务启动完成 ==="
echo "基础MES服务: http://175.27.138.86:8004/admin/"
echo "完整MES服务: http://175.27.138.86:8005/admin/"
echo "Django管理: http://175.27.138.86:8006/admin/"
echo "用户名: admin"
echo "密码: admin123"
echo ""
echo "查看日志:"
echo "tail -f /tmp/mes_8004.log"
echo "tail -f /tmp/mes_8005.log"
echo "tail -f /tmp/mes_8006.log"
echo ""
echo "检查服务状态:"
echo "ps aux | grep 'python manage.py runserver'" 